<template>
  <div>
    <div style="padding: 5px 0">
      <el-button @click="add" type="primary" size="mini" style="margin: 10px">新增</el-button>
    </div>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" sortable></el-table-column>
      <el-table-column prop="type" label="类型">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type === 0">建议</el-tag>
          <el-tag type="danger" v-if="scope.row.type === 1">投诉</el-tag>
          <el-tag type="warning" v-if="scope.row.type === 2">其他</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="comment" label="反馈内容"></el-table-column>
      <el-table-column prop="createTime" label="反馈时间"></el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle  @click="edit(scope.row)"></el-button>
          <el-popconfirm
              @confirm="del(scope.row.id)"
              title="确定删除？"
          >
            <el-button type="danger" icon="el-icon-delete" circle slot="reference" style="margin-left: 10px"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗   -->
    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="30%"
               :close-on-click-modal="false">
      <el-form :model="entity">
        <!-- <el-form-item label="类型" label-width="100px">
          <el-input v-model="entity.type" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item> -->
        <el-form-item label="类型" label-width="100px">
          <el-select v-model="entity.type" placeholder="请选择类型" style="width: 80%">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" label-width="100px">
          <el-input v-model="entity.title" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="反馈内容" label-width="100px">
          <el-input type="textarea" :rows="5" v-model="entity.comment" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import API from '../../utils/request'

export default {
  name: "Feedback",
  data() {
    return {
      url: sessionStorage.getItem("url"),
      fileList: [],
      options: [],
      text: '',
      user: {},
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      entity: {type: 0},
      total: 0,
      dialogFormVisible: false,
      users: [],
      options: [
        { value: '0', label: '建议' },
        { value: '1', label: '投诉' },
        { value: '2', label: '其他' }
      ]
    };
  },
  created() {
    this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    this.$emit('user', this.user);
    this.load()
    API.get("/system/user").then(res => {
      this.users = res.data
    })
  },
  methods: {
    fileSuccessUpload(res) {
      this.entity.file = this.url + res.data;
      this.fileList = [res.data]
      console.log(res)
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    load() {
      if (!this.user.userId) {
        this.$message.warning("请登录")
        return
      }
       API.get("/system/feedback/listAll").then(res => {
          this.tableData = res.rows || []
          this.total = res.total
       })
    },
    add() {
      if (!this.user.userId) {
        this.$message.warning("请登录")
        return
      }
      this.entity = { userId: this.user.userId }
      this.fileList = []
      this.dialogFormVisible = true
    },
    edit(obj) {
      this.entity = JSON.parse(JSON.stringify(obj))
      this.entity.userId = this.user.userId
      this.fileList = []
      this.dialogFormVisible = true
    },
    save() {
      if (!this.entity.id) {
        API.post("/system/feedback", this.entity).then(res => {
           if (res.code === 200) {
             this.$message({
               type: "success",
               message: "操作成功"
             })
           } else {
             this.$message({
               type: "error",
               message: res.msg
             })
           }
           this.load()
           this.dialogFormVisible = false
        })
      } else {
        API.put("/system/feedback", this.entity).then(res => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "操作成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()
          this.dialogFormVisible = false
        })
      }
    },
    del(id) {
      API.delete("/system/feedback/" + id).then(res => {
        this.$message({
          type: "success",
          message: "操作成功"
        })
        this.load()
      })
    }
  },
};
</script>

<style scoped>
</style>
